/*
 * Copyright (C) 2017-2019 Dremio Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.dremio.exec.planner.sql.parser;

import java.util.List;

import org.apache.calcite.sql.SqlAlter;
import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlLiteral;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.SqlSpecialOperator;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.sql.parser.SqlParserPos;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;

/**
 * ALTER CLEAR PLAN CACHE
 */
public class SqlAlterClearPlanCache extends SqlAlter {

  public static final SqlSpecialOperator CLEAR_PLAN_CACHE_OPERATOR = new SqlSpecialOperator("CLEAR_PLAN_CACHE", SqlKind.OTHER) {
    @Override
    public SqlCall createCall(SqlLiteral functionQualifier, SqlParserPos pos, SqlNode... operands) {
      Preconditions.checkArgument(operands.length == 0, "SqlAlterClearPlanCache.createCall() has to get 0 operands!");
      final SqlNode scopeNode = operands[0];
      return new SqlAlterClearPlanCache(pos, scopeNode == null ? null : scopeNode.toString());
    }
  };

  protected final String scope;

  public SqlAlterClearPlanCache(SqlParserPos pos, String scope) {
    super(pos, scope);
    this.scope = scope;
  }

  @Override protected void unparseAlterOperation(SqlWriter writer, int leftPrec, int rightPrec) {
    writer.keyword("CLEAR PLAN CACHE");
    final SqlWriter.Frame frame =
      writer.startList(SqlWriter.FrameTypeEnum.SIMPLE);
    writer.endList(frame);
  }

  @Override public SqlOperator getOperator() {
    return CLEAR_PLAN_CACHE_OPERATOR;
  }

  @Override public List<SqlNode> getOperandList() {
    return Lists.newArrayList();
  }
}
